//
//  EvaluationViewController.m
//  一键车讯
//
//  Created by Scott on 14/10/27.
//  Copyright (c) 2014年 lanou.3g.com. All rights reserved.
//

#import "EvaluationViewController.h"
#import "EvaluationDetailViewController.h"
#import "NetHandler.h"
#import "MJRefresh.h"
#import "Evaluation.h"
#import "EvaluationCell.h"
@interface EvaluationViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, assign)int countx;
@property (nonatomic, retain)NSMutableArray *array;
@end

@implementation EvaluationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)startConnection
{
    [NetHandler networkWithURL:@"http://mrobot.pcauto.com.cn/v2/cms/channels/4?pageNo=1&pageSize=20&v=4.0.0" completion:^(id result) {
        NSMutableArray *dicArr = [result objectForKey:@"data"];
        for (NSDictionary *dic in dicArr) {
            Evaluation *evaluation = [[Evaluation alloc]initWithDictionary:dic];
            [self.array addObject:evaluation];
        }
        [self.tableView reloadData];
        [self setupRefresh];
    }];
}

// 加载视图
- (void)setupView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [_tableView release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"评测";
    [self startConnection];
    [self setupView];
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(barLeftButtonItemAction:)] autorelease];
    
    
}

//UINavigationItem方法
- (void)barLeftButtonItemAction:(UIBarButtonItem *)barButton
{
    // 返回首页
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}

- (void)dealloc
{
    [_tableView release];
    [_array release];
    [super dealloc];
}
#pragma mark 协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"evaluation";
    EvaluationCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[EvaluationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str] autorelease];
    }
    Evaluation *evaluation = [self.array objectAtIndex:indexPath.row];
    cell.evaluation = evaluation;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    EvaluationDetailViewController *EvaluVC = [[EvaluationDetailViewController alloc] init];
    EvaluVC.evaluation = [self.array objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:EvaluVC animated:YES];
    [EvaluVC release];
}

- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    [self.tableView footerEndRefreshing];
    //
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.tableView.headerPullToRefreshText = @"下拉以刷新...";
    self.tableView.headerReleaseToRefreshText = @"松开即可刷新...";
    self.tableView.headerRefreshingText = @"刷新中...";
    
    self.tableView.footerPullToRefreshText = @"上拉以加载更多数据";
    self.tableView.footerReleaseToRefreshText = @"松开马上加载更多数据";
    self.tableView.footerRefreshingText = @"加载中";
    
    self.countx = 2;
    NSLog(@"se.countx %d", self.countx);
}
#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        // 刷新表格
        [self.tableView reloadData];
        
        [_tableView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.tableView headerEndRefreshing];
    });
}

- (void)footerRereshing
{
    //_dataSource = [[NSMutableArray alloc]init];
    
    //    self.countx += 1;
    NSString *str = [NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v2/cms/channels/4?pageNo=%d&pageSize=20&v=4.0.0", self.countx];
    [NetHandler networkWithURL:str completion:^(id result) {
        NSMutableArray *dicArr = [result objectForKey:@"data"];
        for (NSDictionary *dic in dicArr) {
            Evaluation *evaluation = [[Evaluation alloc]initWithDictionary:dic];
            [self.array addObject:evaluation];
        }
        [self.tableView reloadData];
        
    }];
    
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.tableView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.tableView footerEndRefreshing];
        _countx += 1;
//        NSLog(@"self.countx %d", self.countx);
    });
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
